package com.example.mymvp.view.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.example.mymvp.view.model.BaseModel;
import com.example.mymvp.view.presenter.BasePresenter;
import com.example.mymvp.view.view.BaseView;

/**
 * @Name：day07_demo
 * @Description：描述信息
 * @Author：86188 范嘉豪
 * @Date：2021/3/17 19:47
 * 修改人：86188 范嘉豪
 * 修改时间：2021/3/17 19:47
 * 修改备注：
 */
public  abstract class BaseLazyFramgent<V extends BaseView,M extends BaseModel,P extends BasePresenter<V,M>> extends BaseFragment {
    private boolean isFirst=false;//第一次加载
    private boolean isUserVisible=false;//不可见
    private boolean isLoading=true;//未加载过数据
    P mPresenter;
    //第一次加载

    //第二次加载

    //第三次加载

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser){
            isUserVisible=true;
        }else{
            isUserVisible=false;
        }
        lazyLoad();
    }

    private void lazyLoad() {
        if(isFirst&&isUserVisible&&isLoading){
            mPresenter=createPresenter();
            if(mPresenter!=null){
                mPresenter.bindView((V) this);
            }
            lazyData();
            isLoading=false;
        }
    }

    protected abstract P createPresenter();

    protected abstract void lazyData();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(mPresenter!=null){
            mPresenter.destoryView();
            mPresenter=null;
        }
        isFirst=false;
        isUserVisible=false;
        isLoading=true;

    }
}
